package com.wyh.lotterysystem.service.activitystatus.operator;

import com.wyh.lotterysystem.dao.dataobject.ActivityDo;
import com.wyh.lotterysystem.dao.dataobject.ActivityPrizeDo;
import com.wyh.lotterysystem.dao.mapper.ActivityMapper;
import com.wyh.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.wyh.lotterysystem.dao.mapper.ActivityUserMapper;
import com.wyh.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.wyh.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import com.wyh.lotterysystem.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PrizeOperator extends AbstractActivityOperator{

    @Autowired
    ActivityMapper activityMapper;

    @Autowired
    ActivityUserMapper activityUserMapper;

    @Autowired
    ActivityPrizeMapper activityPrizeMapper;

    @Override
    public int sequence() {
        return 1;
    }

    @Override
    public boolean convertStatus(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            activityPrizeMapper.updateStatus(convertActivityStatusDTO.getActivityId(),
                    convertActivityStatusDTO.getPrizeId(),
                    convertActivityStatusDTO.getTargetActivityPrizeStatus().name());
            return true;
        }catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Integer targetId = convertActivityStatusDTO.getActivityId();
        Integer prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetStatus = convertActivityStatusDTO.getTargetActivityPrizeStatus();

        if (null == targetStatus
                || null == targetId
                || null == prizeId){
            return false;
        }

        //奖品不存在
        ActivityPrizeDo activityPrizeDo = activityPrizeMapper.selectByActivityPrizeIdAndActivityId(targetId,prizeId);
        if (null == activityPrizeDo){
            return false;
        }

        //奖品状态与目标不同
        if (activityPrizeDo.getStatus().equalsIgnoreCase(targetStatus.name())){
            return false;
        }

        return true;
    }
}
